package hongvt.quanlynhanvien.utils;

import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesManager {
	private static final String PREFERENCES_NAME = "PREFERENCES_NAME";

	private static SharedPreferencesManager instance = null;
	private Context context;

	/**
	 * Constructor
	 */
	private SharedPreferencesManager() {
	}

	/**
	 * Get class instance
	 * 
	 * @param context
	 * @return
	 */
	public static SharedPreferencesManager getInstance(Context context) {
		if (instance == null) {
			instance = new SharedPreferencesManager();
			instance.context = context;
		}
		return instance;
	}

	/**
	 * Save an string to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putStringValue(String key, String s) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(key, s);
		editor.commit();
	}

	/**
	 * Read an string to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public String getStringValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		return pref.getString(key, "");
	}

	/**
	 * Read an string to SharedPreferences
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getStringValue(String key, String defaultValue) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		return pref.getString(key, defaultValue);
	}

	/**
	 * Save an boolean to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putBooleanValue(String key, Boolean b) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(key, b);
		editor.commit();
	}

	/**
	 * Read an boolean to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public boolean getBooleanValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		return pref.getBoolean(key, false);
	}

	/**
	 * Read an boolean to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public boolean getBooleanValue(String key, boolean defaulValue) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		return pref.getBoolean(key, defaulValue);
	}

	/**
	 * Save an float to SharedPreferences
	 * 
	 * @param key
	 * @param s
	 */
	public void putFloatValue(String key, float f) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putFloat(key, f);
		editor.commit();
	}

	/**
	 * Read an float to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public float getFloatValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		return pref.getFloat(key, 0.0f);
	}

	/**
	 * Save an int to SharedPreferences
	 * 
	 * @param key
	 * @param value
	 */
	public void putIntValue(String key, int value) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	/**
	 * Read an int to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public int getIntValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		return pref.getInt(key, 0);
	}

	/**
	 * Read an int to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public int getIntValue(String key, int defaultValue) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				defaultValue);
		return pref.getInt(key, defaultValue);
	}

	/**
	 * Save an long to SharedPreferences
	 * 
	 * @param key
	 * @param value
	 */
	public void putLongValue(String key, long value) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		SharedPreferences.Editor editor = pref.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	/**
	 * Read an long to SharedPreferences
	 * 
	 * @param key
	 * @return
	 */
	public long getLongValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		return pref.getLong(key, 0);
	}


	public void removeAValue(String key) {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		SharedPreferences.Editor editor = pref.edit();
		editor.remove(key);
		editor.commit();
	}

	public void removeAll() {
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
				0);
		SharedPreferences.Editor editor = pref.edit();
		editor.clear();
		editor.commit();
	}

}
